#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>

#define max 1

typedef struct Leaf
{
	char name[max];
	Leaf * cl;
	Leaf * cr;
}leaf,*pleaf;

typedef struct zhan
{
	pleaf x;
	zhan * next;
}dian,*pdian;

void initdian(pdian &pz);
pdian weidian(pdian pz);
void weijiadian(pdian pz,pleaf ptree);
void toujiadian(pdian pz,pleaf ptree);
pdian fudian(pdian pz,pdian d);
void weishandian(pdian pz);
void toushandian(pdian pz);
void precreate(pleaf &ptree);
void preprint(pleaf ptree);
void aoru(pleaf ptree,int indent);
int main()
{
	pleaf ptree=NULL;
	precreate(ptree);
	preprint(ptree);
}
void aoru(pleaf ptree,int indent)
{
	if(ptree){
		for(int i=0;i<indent;i++)
			printf(" ");
		printf("%s\n",ptree->name );
		aoru();
	}
}

void preprint(pleaf ptree)
{
	if(ptree){
		printf("%s ",ptree->name );
		preprint(ptree->cl );
		preprint(ptree->cr );
	}
}

void precreate(pleaf &ptree)
{
	char name[max];
	scanf("%s",name);
	if(strcmp(name,",")==0)
		ptree=NULL;
	else{
		ptree=new leaf;
		strcpy(ptree->name ,name);
		precreate(ptree->cl );
		precreate(ptree->cr );
	}
}

void toushandian(pdian pz)
{
	pdian a=pz->next ;
	pdian b=a->next ;
	pz->next =b;
	delete a;
}

void weishandian(pdian pz)
{
	pdian a=NULL;
	a=weidian(pz);
	pdian b=NULL;
	b=fudian(pz,a);
	b->next =NULL;
	delete a;
}

pdian fudian(pdian pz,pdian d)
{
	if(pz){
		pdian a=pz->next;
		while(a){
			if(a==d){
				return pz;
			}
			pz=pz->next ;
			a=pz->next ;
		}
	}
}

void toujiadian(pdian pz,pleaf ptree)
{
	pdian a=NULL;
	initdian(a);
	a->x =ptree;
	pdian b=pz->next ;
	pz->next =a;
	a->next =b;
}

void weijiadian(pdian pz,pleaf ptree)
{
	pdian a=NULL;
	initdian(a);
	a->x =ptree;
	pdian b=NULL;
	b=weidian(pz);
	b->next =a;
}

pdian weidian(pdian pz)
{
	while(pz->next )
	{
		pz=pz->next ;
	}
	return pz;
}

void initdian(pdian &pz)
{
	pz=new dian;
	pz->x =NULL;
	pz->next =NULL;
}